from huey.exceptions import TaskException

from src.gen_file.datamodel import GenModel
from src.gen_file.gen_SOPfile import GenSOPFile
from src.gen_file.gen_BPRfile import GenBPRFile
from src.gen_file.gen_BPRfile_multibag import GenMultiBPRFile
from src.huey_init._huey import huey
from src.tools.dms_operator import dmsoperator
from src.tools.file_type import check_file_type1, check_file_type2

@huey.task(context=True)
def run_gen_task(gen_model: GenModel, task=None):
    file_name = gen_model.file_list[0]["format_ref_file_info"].get("documentName")
    file_path = gen_model.file_list[0]["format_ref_file_info"].get("realPath")
    # 根据文件名和文件内容判断任务是生成工艺规程还是批记录
    if "工艺规程" in file_name or "工艺规程" in check_file_type1(file_path):
        run_gensop_task(gen_model, task)
    elif "记录" in file_name or "生产批记录" in check_file_type1(file_path):
        run_genonemultibpr_task(gen_model, task)
    else:
        errorinfo = {
            "FunctionType": gen_model.exchange_info.get("functionType"),
            "TaskId": gen_model.exchange_info.get("dmsTaskId"),
            "AiTaskId": task.id,
            "Reason": "没有工艺规程或批记录"
        }
        dmsoperator.post_errortodms(errorinfo=errorinfo)
        raise TaskException("没有工艺规程或批记录")



def run_gensop_task(gen_model: GenModel, task=None):
    gen_sop = GenSOPFile()
    for info in gen_model.file_list:
        # 执行生成工艺规程
        res = gen_sop.gen_SOPfile(info["prod_line_info"], info["content_cit_file_list"],
                                  info["format_ref_file_info"], info["template_info"], task.id, gen_model.exchange_info)
        # 执行成功回调dms
        exchange_info = {
            "functionType": gen_model.exchange_info.get("functionType"),
            "taskType": gen_model.exchange_info.get("taskType"),
            "taskDocumentType": gen_model.exchange_info.get("taskDocumentType"),
            "executionType": gen_model.exchange_info.get("executionType"),
            "isSuccess": True,
            "dmsTaskId": gen_model.exchange_info.get("dmsTaskId"),
            "docId": info["format_ref_file_info"].get("docId"),
            "taskDocId": info["format_ref_file_info"].get("taskDocId"),
            "resultFirstFilePath": res[0],
            "resultSecondFilePath": None,
            "aiTaskId": task.id,
            "aiGenFileType": 0,
            "taskDocName": res[1]
        }
        dmsoperator.post_exchange_info(exchange_info)

def run_genonemultibpr_task(gen_model: GenModel, task=None):
    multi_bag = False
    for info in gen_model.file_list:
        for content_cit_file in info["content_cit_file_list"]:
            # 根据文件名判断是生成多室袋批记录还是批记录
            content_cit_file_path = content_cit_file.get("realPath")
            if "室袋" in content_cit_file.get("documentName") or "多室袋工艺规程" in check_file_type2(content_cit_file_path):
                multi_bag = True
    if multi_bag == True:
        run_genmultibpr_task(gen_model, task)
    else:
        run_genbpr_task(gen_model, task)

def run_genmultibpr_task(gen_model: GenModel, task=None):
    gen_bpr = GenMultiBPRFile()
    bproper_num = {}
    for bpeoper in ["称量", "配制"]:
        bproper_num[bpeoper] = 0
    for info in gen_model.file_list:
        # 执行生成多室袋批记录
        res = gen_bpr.gen_BPRfile(info["content_cit_file_list"], info["format_ref_file_info"],
                                  info["template_info"], task.id, gen_model.exchange_info, bproper_num)
        bproper_num = res[2]
        # 执行成功回调dms
        exchange_info = {
            "functionType": gen_model.exchange_info.get("functionType"),
            "taskType": gen_model.exchange_info.get("taskType"),
            "taskDocumentType": gen_model.exchange_info.get("taskDocumentType"),
            "executionType": gen_model.exchange_info.get("executionType"),
            "isSuccess": True,
            "dmsTaskId": gen_model.exchange_info.get("dmsTaskId"),
            "docId": info["format_ref_file_info"].get("docId"),
            "taskDocId": info["format_ref_file_info"].get("taskDocId"),
            "resultFirstFilePath": res[0],
            "resultSecondFilePath": None,
            "aiTaskId": task.id,
            "aiGenFileType": 1,
            "taskDocName": res[1]
        }
        dmsoperator.post_exchange_info(exchange_info)


def run_genbpr_task(gen_model: GenModel, task=None):
    gen_bpr = GenBPRFile()
    for info in gen_model.file_list:
        # 执行生成批记录
        res = gen_bpr.gen_BPRfile(info["content_cit_file_list"], info["format_ref_file_info"],
                                  info["template_info"], task.id, gen_model.exchange_info)
        # 生成成功回调dms
        exchange_info = {
            "functionType": gen_model.exchange_info.get("functionType"),
            "taskType": gen_model.exchange_info.get("taskType"),
            "taskDocumentType": gen_model.exchange_info.get("taskDocumentType"),
            "executionType": gen_model.exchange_info.get("executionType"),
            "isSuccess": True,
            "dmsTaskId": gen_model.exchange_info.get("dmsTaskId"),
            "docId": info["format_ref_file_info"].get("docId"),
            "taskDocId": info["format_ref_file_info"].get("taskDocId"),
            "resultFirstFilePath": res[0],
            "resultSecondFilePath": None,
            "aiTaskId": task.id,
            "aiGenFileType": 1,
            "taskDocName": res[1]
        }
        dmsoperator.post_exchange_info(exchange_info)
